import 'package:mall/constant/app_parameters.dart';
import 'package:mall/constant/app_strings.dart';
import 'package:mall/constant/app_urls.dart';
import 'package:mall/modules/model/category_goods_entity.dart';
import 'package:mall/modules/model/goods_detail_entity.dart';
import 'package:mall/modules/model/search_goods_entity.dart';
import 'package:mall/utils/broadcast/broadcast_util.dart';
import 'package:mall/utils/net/http_util.dart';

import '../model/collection_entity.dart';

//商品修改
abstract class GoodsEvent {
  // 收藏或者取消收藏
  void addOrDeleteCollect(int goodsId);
}

//订单事件
abstract class OrderEvent {
  // 购买了商品
  void buy(int goodsId, int productId, int number);
  //提交了新订单
  void submitOrder(
    String cartId,
    String addressId,
    String message,
    String couponId,
  );
}

class GoodsService {
// 获取分类下的商品信息
  static Future<CategoryGoodsEntity> getCategoryGoodsData(int categoryId, int pageIndex, int limit) async {
    var parameters = {AppParameters.CATEGORY_ID: categoryId, AppParameters.PAGE: pageIndex, AppParameters.LIMIT: limit};

    var response = await HttpUtil.instance.get(AppUrls.GOODS_LIST_URL, parameters: parameters);

    if (response[AppStrings.DATA] == null) {
      throw Exception(AppStrings.SERVER_EXCEPTION);
    }

    return CategoryGoodsEntity.fromJson(response[AppStrings.DATA]);
  }

// 获取商品详情
  static Future<GoodsDetailEntity> getGoodsDetailData(int goodsId) async {
    var parameters = {AppParameters.ID: goodsId};
    var response = await HttpUtil.instance.get(AppUrls.GOODS_DETAILS_URL, parameters: parameters);

    if (response[AppStrings.DATA] == null) {
      throw Exception(AppStrings.SERVER_EXCEPTION);
    }

    return GoodsDetailEntity.fromJson(response[AppStrings.DATA]);
  }

// 快速购买
  static Future<int> buy({
    required int goodsId,
    required int productId,
    required int number,
  }) async {
    var parameters = {
      AppParameters.GOODS_ID: goodsId,
      AppParameters.PRODUCT_ID: productId,
      AppParameters.NUMBER: number,
      AppParameters.GROUPON_RULES_ID: 0,
      AppParameters.GROUPON_LINK_ID: 0
    };

    var response = await HttpUtil.instance.post(AppUrls.FAST_BUY, parameters: parameters);

    final data = response[AppStrings.DATA];

    if (data == null || data is! int) {
      throw Exception(AppStrings.SERVER_EXCEPTION);
    }
    return data;

    // BroadcastUtil.notify<OrderEvent>((p0) => p0.buy(goodsId, productId, number));
  }

// 查询收藏
  static Future<CollectionEntity> queryCollect(int type, int pageIndex, int limit) async {
    var parameters = {
      AppParameters.TYPE: type,
      AppParameters.PAGE: pageIndex,
      AppParameters.LIMIT: limit,
    };

    var response = await HttpUtil.instance.get(AppUrls.MINE_COLLECT, parameters: parameters);

    if (response[AppStrings.DATA] == null) {
      throw Exception(AppStrings.SERVER_EXCEPTION);
    }

    return CollectionEntity.fromJson(response[AppStrings.DATA]);
  }

// 收藏或者取消收藏
  static Future<void> addOrDeleteCollect(int goodsId) async {
    var parameters = {AppParameters.TYPE: 0, AppParameters.VALUE_ID: goodsId};
    await HttpUtil.instance.post(
      AppUrls.COLLECT_ADD_DELETE,
      parameters: parameters,
    );
    BroadcastUtil.notify<GoodsEvent>((p0) => p0.addOrDeleteCollect(goodsId));
  }

// 提交订单
  static Future<void> submitOrder({
    required String cartId,
    required String addressId,
    required String message,
    required String couponId,
  }) async {
    var parameters = {
      AppParameters.CART_ID: cartId,
      AppParameters.ADDRESS_ID: addressId,
      AppParameters.MESSAGE: message,
      AppParameters.COUPON_ID: couponId,
      AppParameters.GROUPON_RULES_ID: 0,
      AppParameters.GROUPON_LINK_ID: 0
    };

    var response = await HttpUtil.instance.post(AppUrls.SUBMIT_ORDER, parameters: parameters);

    if (response[AppStrings.DATA] == null) {
      throw Exception(AppStrings.SERVER_EXCEPTION);
    }

    BroadcastUtil.notify<OrderEvent>((p0) => p0.submitOrder(cartId, addressId, message, couponId));
  }

// 商品搜索
  static Future<SearchGoodsEntity> searchGoods({
    String? keyword = '',
    required int pageIndex,
    required int limit,
    String? sortName,
    String? orderType,
  }) async {
    var parameters = {
      AppParameters.KEYWORD: keyword,
      AppParameters.PAGE: pageIndex,
      AppParameters.LIMIT: limit,
      AppParameters.SORT: sortName,
      AppParameters.ORDER: orderType
    };

    var response = await HttpUtil.instance.get(AppUrls.GOODS_LIST_URL, parameters: parameters);

    if (response[AppStrings.DATA] == null) {
      throw Exception(AppStrings.SERVER_EXCEPTION);
    }

    return SearchGoodsEntity.fromJson(response[AppStrings.DATA]);
  }
}
